<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>第一题</title>
</head>

<body>
    <script>
        const goodsList = [
            {
                id: '4001172',
                name: '张三',
                price: 289.9,
                count: 2,
                spec: { color: '白色' }
            },
            {
                id: '4001009',
                name: '李四',
                price: 109.8,
                count: 3,
                spec: { size: '40cm*40cm', color: '黑色' }
            },
            {
                id: '4001874',
                name: '老六',
                price: 488,
                count: 1,
                spec: { color: '青色', sum: '一大四小' }
            },
            {
                id: '4001649',
                name: '老六',
                price: 139,
                count: 1,
                spec: { size: '小号', color: '紫色' },
                gift: '50g茶叶,清洗球,宝马, 奔驰'
            }
        ]
        // /封装函数 找到老六这个对象 返回整个对象出来
        function getPenson(arr) {
            arr.forEach((item) => {
                if (item.name === '老六') {
                    console.log(item)
                }
            })
        }
        getPenson(goodsList)
        // /封装函数 找到老六这俩项 筛选出来
        function getPenson1(arr1) {
            console.log(arr1)
            let arr = []
            arr = arr1.filter((item) => {
                return item.name === '老六'
            })
            console.log(arr)
        }
        getPenson1(goodsList)
        // console.log(getPenson1(goodsList));
        // /封装函数 把所有的价格累加
        function getPenson3(arr) {
            let sum = 0
            arr.forEach((item) => {
                sum += item.price
            })
            console.log(sum)
        }
        getPenson3(goodsList)
        // /数组对象解构出来
        function getData(data) {
            data.forEach(function (item) {
                // console.log(item[0]) 
                if (item.id === '4001172') {
                    const { id, name, price, count, spec, spec: { color } } = item
                    console.log(color)
                } else if (item.id === '4001009') {
                    const { id, name, price, count, spec, spec: { size, color } } = item
                } else if (item.id === '4001874') {
                    const { id, name, price, count, spec, spec: { color, sum } } = item
                } else if (item.id === '4001649') {
                    const { id, name, price, count, spec, spec: { color, sum }, gift } = item
                    console.log(gift)
                }
            })
        }
        getData(goodsList)
    </script>
</body>

</html>